<template>
  <el-dialog :visible.sync="dialogVisible" class="app-container" :title="formTitle" width="60%">
    <el-form ref="formCom" :model="form" label-width="120px" :inline="true" :label-position="labelPosition" :rules="rules">
      <el-form-item label="课程名称">
        <el-input v-model="form.courseId" class="formItem" />
        <!-- <el-select v-model="form.courseName" multiple placeholder="请选择课程" class="formItem">
          <el-option v-for="(courseName,index) in courseNames" :key="index" :label="courseName" :value="courseName" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="班级名称">
        <el-input v-model="form.classId" class="formItem" />
        <!-- <el-select v-model="form.listClassByUserId" multiple placeholder="请选择班级" class="formItem" @change="listCourseByUserIdAndClassId">
          <el-option v-for="(className,index) in classNames" :key="index" :label="className" :value="className" />
        </el-select> -->
      </el-form-item>
      <el-form-item label="试卷名称" prop="paperName">
        <el-input v-model="form.paperName" class="formItem" />
      </el-form-item>
      <el-form-item label="试卷总时长" prop="paperTime">
        <el-input v-model="form.paperTime" class="formItem" />
      </el-form-item>
      <el-form-item label="试卷总分" prop="paperScore">
        <el-input v-model="form.paperScore" class="formItem" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <div class="btn_opt">
        <el-button @click="onCancel">取 消</el-button>
        <el-button type="primary" @click="doSave">确 定</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { addCourse } from '@/api/course'
import { listClassByUserId, listCourseByUserIdAndClassId } from '@/api/class'

import fileRequest from '@/utils/fileRequest'
import { createPaper } from '@/api/paper'
export default {
  data() {
    return {
      formTitle: '编辑用户',
      dialogVisible: false,
      labelPosition: 'right',
      isAddOPt: true,
      classNames: [],
      courseNames:[],
      resourceNum: 0,
      fileList: [],
      uploadUrl: fileRequest.fileUploadUrl,
      dateFormat: 'yyyy-MM-dd hh:mm:ss',
      form: {
        courseResources: [{}]
      },
      defaultForm: {
        courseResources: [{}]
      },
      rules: {
        paperName: [
          { required: true, message: '试卷名称不能为空', trigger: 'blur,change' }
        ],
        paperTime: [
          { required: true, message: '试卷时间不能为空', trigger: 'change' }
        ],
        paperScore: [
          { required: true, message: '试卷总分不能为空', trigger: 'change' }
        ]
      }
    }
  },
  mounted() {
    this.listClassesByUserId()
  },
  methods: {
    onSubmit() {
      this.$message('submit!')
    },
    showDialog() {
      this.dialogVisible = true
    },
    async  listCourseByUserIdAndClassId() {
      const cn = this.form.className
      // 如果是多选下拉框，将数组转换为逗号分隔的字符串
      if (Array.isArray(cn)) {
        cn = cn.join(',');
      }
      const data = await listCourseByUserIdAndClassId(cn)
      const courses = data.data
      console.log(">>>>>>>>>>>>>>"+courses)
      for (let i = 0; i < courses.length; i++) {
        this.courseNames.push(courses[i].courseName)
      }
    },
    async  listClassesByUserId() {
      const data = await listClassByUserId()
      const classes = data.data
      for (let i = 0; i < classes.length; i++) {
        this.classNames.push(classes[i].className)
      }
    },
    uploadPPTSuccess(response, file, fileList) {
      const data = response.data
      //   resourceType=1, ppt、word资源
      const resource = { resourcePath: data, resourceType: 1, resourceDes: 'ppt、word资源' }
      this.form.courseResources.push(resource)
    },
    uploadVideoSuccess(response, file, fileList) {
      const data = response.data
      //   resourceType=2, 视频资源
      const resource = { resourcePath: data, resourceType: 2, resourceDes: '视频资源' }
      this.form.courseResources.push(resource)
    },
    async editForm(userId) {
      this.formTitle = '修改课程'
      // const data = await getUserInfoById(userId)
      // this.form = data.data
      this.isAddOPt = true
      this.dialogVisible = true
    },
    addForm() {
      this.formTitle = '添加课程'
      this.reset()
      this.isAddOPt = true
      this.dialogVisible = true
    },
    checkParam(formName) {
      let pass = true
      this.$refs[formName].validate((valid) => {
        if (valid) {
          pass = true
        } else {
          console.log('请先填写完表单必填选!!')
          this.$message.error('请先填写完表单必填选!!')
          pass = false
        }
      })
      return pass
    },
    async doSave() {
      let data = ''
      const pass = this.checkParam('formCom')
      // 打印请求数据
      console.log('发送的请求数据：', this.form)
      if (!pass) {
        return
      }

      if (this.isAddOPt) {
        data = await createPaper(this.form)
        this.$message.success('创建试卷成功！')
      }
      this.dialogVisible = false
      this.$emit('refreshDataList')
    },
    reset() {
      this.form = this.defaultForm
    },
    onCancel() {
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>
  .formItem{
    width: 200px;
  }
.line{
  text-align: center;
}
</style>

